package com.example.mrzhou.srtfoodapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by SRTfood on 2017/1/17.
 */

public class LoginActivity extends BaseActivity{

    private SharedPreferences pref;

    private SharedPreferences.Editor editor;

    private EditText accountEdit;

    private EditText passwordEdit;

    private Button login;

    private CheckBox rememberPass;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subscriber_login);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        accountEdit = (EditText) findViewById(R.id.account);
        passwordEdit = (EditText) findViewById(R.id.password);
        rememberPass = (CheckBox) findViewById(R.id.remember_pass);
        login = (Button) findViewById(R.id.login);
        boolean isRemember = pref.getBoolean("remember_password",false);
        if (isRemember){
            //将账号和密码都设置到文本框中
            String account = pref.getString("account","");
            String password = pref.getString("password","");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberPass.setChecked(true);
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i;
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                //  Subscriber subscriber[]=new Subscriber[100];
                //  subscriber[0].setAccount("admin");
                //  subscriber[0].setPassword("123456");
                //  for (i=0;i<=100;i++) {
                    if (account.equals("admin")
                            && password.equals("123456")) {
                        editor = pref.edit();
                        if (rememberPass.isChecked()) {//检查复选框是否被选中
                            editor.putBoolean("remember_password", true);
                            editor.putString("account", account);
                            editor.putString("password", password);
                        } else {
                            editor.clear();
                        }
                        editor.commit();
                        Intent intent = new Intent(LoginActivity.this,SRTfoodMainActivity.class);
                        startActivity(intent);
                        finish();
                        //   break;
                    } else {
                        Toast.makeText(LoginActivity.this,"account or password" +
                                " is invalid",Toast.LENGTH_SHORT).show();
                    }
                }
           // }
        });
    }
}
